﻿using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using CrystalDecisions.Shared;
using CrystalDecisions.CrystalReports.Engine;
using CrystalDecisions.Web;
using System.Data.SqlClient;
using Microsoft.ApplicationBlocks.Data;

public partial class CrystalReport_GetReduceSituationList : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        if (!IsPostBack)
            BindDate();
        if (Session["UserType"].ToString() != "住房保障管理中心")
        {
            int year = Convert.ToInt32(ddlyear.SelectedValue);
            int month = Convert.ToInt32(ddlmonth.SelectedValue);
            int time = year * 100 + month;
            string[] arrayadd = new string[8] { "江厦", "月湖", "南门", "鼓楼", "西门", "白云", "望春", "段塘" };
            DataSetCrystal ds = new DataSetCrystal();
            string connString = ConfigurationManager.ConnectionStrings["SqlConnection"].ConnectionString;
            for (int j = 0; j < 8; j++)
            {
                string cmdtext = @"SELECT COUNT(TAB.adj_longid) AS C,SUM(TAB.adj_nowmoney) AS S FROM (SELECT DISTINCT adj_longid,adj_nowmoney,
                                (SELECT apply_street FROM Apply WHERE apply_longid=R.adj_longid) AS street FROM ReduceRecord AS R
                                WHERE adj_starttime<=" + time + " AND adj_stoptime>" + time + ") AS TAB " +
                                "WHERE TAB.street='" + arrayadd[j] + "'";
                //int i = Convert.ToInt32(SqlHelper.ExecuteScalar(connString, CommandType.Text, cmdtext));
                SqlDataReader sqldr = SqlHelper.ExecuteReader(connString, CommandType.Text, cmdtext);
                DataTable dt = ds.Tables["GetReduceSituationList"];
                DataRow dr = dt.NewRow();
                dr[0] = arrayadd[j];
                if (sqldr.Read())
                {
                    dr[1] = sqldr["C"].ToString();
                    if (!string.IsNullOrEmpty(sqldr["S"].ToString()))
                        dr[2] = float.Parse(sqldr["S"].ToString());
                    else
                        dr[2] = 0;
                }
                sqldr.Close();
                dt.Rows.Add(dr);
            }
            //使用报表对象加载报表
            ReportDocument myReport = new ReportDocument();
            string reportPath = Server.MapPath("~/CrystalReport/GetReduceSituationList.rpt");
            myReport.Load(reportPath);
            //绑定数据集，注意，一个报表用一个数据集。
            myReport.SetDataSource(ds);
            CrystalReportViewer1.ReportSource = myReport;

            ParameterFields paramFields = new ParameterFields();
            ParameterField paramField = new ParameterField();
            ParameterDiscreteValue pdValue = new ParameterDiscreteValue();

            paramField.ParameterFieldName = "Time";
            pdValue.Value = ddlyear.SelectedValue + "年" + ddlmonth.SelectedValue + "月";
            paramField.CurrentValues.Add(pdValue);
            paramFields.Add(paramField);

            CrystalReportViewer1.ParameterFieldInfo = paramFields;
        }
        else
            WebHelper.AlertAndRedirect("登陆超时或无权访问","../Login.aspx");
    }
    public void BindDate()
    {
        int year = Convert.ToInt32(DateTime.Now.Year.ToString());
        for (int i = year - 2; i < year + 3; i++)
        {
            ddlyear.Items.Add(i.ToString());
        }
        ddlyear.SelectedValue = DateTime.Now.Year.ToString();
        ddlmonth.SelectedValue = DateTime.Now.Month.ToString();
    }
}
